//
//  SunBankManagerVC.m
//  Xspcf
//
//  Created by sunshine on 16/10/27.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunBankManagerVC.h"
#import "SunBankCardCell.h"
#import "SunOpenBindVC.h"

@interface SunBankManagerVC ()
{
    UILabel *_imgBank1;
    UILabel *_imgBank2;
    UILabel *_imgCard1;
    UILabel *_imgCard2;
    UILabel *_name;
    UILabel *_jxAccount;
    UILabel *_jxBank;
    UILabel *_bank;
    UIButton *_btn;
}
@end

@implementation SunBankManagerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"银行卡管理";
    self.view.backgroundColor = BACKGROUND;
    [self creatUI];
    [self updateViews];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestToUser];
}

- (void)creatUI{
    UIView *msg = [[UIView alloc]init];
    [self.view addSubview:msg];
    [msg lt_setTop:0 to:nil save:YES];
    [msg lt_setLeft:0 to:nil Right:0 to:nil save:YES];
    msg.backgroundColor = [HXColor colorWithHexString:@"FAEFBF"];
    UILabel *red = [[UILabel alloc]init];
    [msg addSubview:red];
    [red lt_setSize:CGSizeMake(X(18), X(18)) save:YES];
    [red lt_setTop:Y(12) to:nil save:YES];
    [red lt_setLeft:X(16) to:nil save:YES];
    red.clipsToBounds = YES;
    red.backgroundColor = RED;
    red.text = @"!";
    red.font = [UIFont boldSystemFontOfSize:14];
    red.textAlignment = NSTextAlignmentCenter;
    red.textColor = WHITE;
    red.layer.cornerRadius = X(9);
    UIView *bank = [[UIView alloc]init];
    [self.view addSubview:bank];
    [bank lt_Top:SunEdgesMake(0, 0, 0, Y(130)) to:msg save:YES];
    bank.backgroundColor = BLACK;
    UIImageView *bg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, X(300), Y(60))];
    bg.layer.cornerRadius = 5;
    [bank addSubview:bg];
    [bg lt_All:SunEdgesMake(15, 10, 10, 10) save:YES];
    bg.image = [Sun imageWithColorFrom:[HXColor colorWithHexString:@"E34D79"] To:[HXColor colorWithHexString:@"E95268"] Frame:bg.frame Direction:SunDirectionDiagonal0];
    bg.clipsToBounds = YES;
    bg.layer.borderColor = BLUE.CGColor;
    UIImageView *logo = [[UIImageView alloc]init];
    [bg addSubview:logo];
    [logo lt_setSize:CGSizeMake(X(34), Y(34)) save:YES];
    [logo lt_setTop:Y(15) to:nil save:YES];
    [logo lt_setLeft:X(15) to:nil save:YES];
    UILabel *title = [[UILabel alloc]init];
    [bg addSubview:title];
    [title lt_YcenterTo:logo save:YES];
    [title lt_setLeft:X(8) to:logo save:YES];
    title.textColor = WHITE;
    title.font = [UIFont systemFontOfSize:16];
    UILabel *bankNum = [[UILabel alloc]init];
    [bg addSubview:bankNum];
    [bankNum lt_leftEqual:title save:YES];
    [bankNum lt_setTop:Y(24) to:logo save:YES];
    bankNum.textColor = WHITE;
    bankNum.font = [UIFont systemFontOfSize:19];
    UIImageView *defaut = [[UIImageView alloc]init];
    [bg addSubview:defaut];
    [defaut lt_setSize:CGSizeMake(X(30), Y(25)) save:YES];
    [defaut lt_topEqual:nil save:YES];
    [defaut lt_setRight:X(15) to:nil save:YES];
    UILabel *phoneNum = [[UILabel alloc]init];
    [bg addSubview:phoneNum];
    [phoneNum lt_rightEqual:defaut save:YES];
    [phoneNum lt_setTop:Y(7) to:defaut save:YES];
    phoneNum.textColor = WHITE;
    phoneNum.font = [UIFont systemFontOfSize:13];
    defaut.image = [UIImage imageNamed:@"third_bank_default"];
    defaut.hidden = YES;
    NSDictionary *dic = [Sun getUser:USER];
    title.text = [dic objectForKey:@"bank"];
    phoneNum.text = [NSString stringWithFormat:@"手机尾号%@",[[dic objectForKey:@"phone"]substringFromIndex:7]];
    bankNum.text = [NSString stringWithFormat:@"*** **** **** %@",[[dic objectForKey:@"bankCard"]substringFromIndex:[[dic objectForKey:@"bankCard"]length] - 4]];
    [logo sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KIMG,[dic objectForKey:@"bankImg"]]]];
    UIImageView *defautImage = [[UIImageView alloc]init];
    [bg addSubview:defautImage];
    [defautImage lt_setSize:CGSizeMake(X(30), Y(25)) save:YES];
    [defautImage lt_topEqual:nil save:YES];
    [defautImage lt_setRight:X(15) to:nil save:YES];
    defautImage.image = [UIImage imageNamed:@"third_bank_default"];


    UIView *detail = [[UIView alloc]init];
    [self.view addSubview:detail];
    [detail lt_setTop:0 to:bank save:YES];
    [detail lt_setLeft:0 to:nil Right:0 to:nil save:YES];
    detail.backgroundColor = WHITE;
    
    UIView *intro = [[UIView alloc]init];
    [detail addSubview:intro];
    [intro lt_Top:SunEdgesMake(0, 0, 0, Y(54)) to:nil save:YES];
    UIView *point1 = [[UIView alloc]init];
    [intro addSubview:point1];
    [point1 lt_YcenterTo:nil save:YES];
    [point1 lt_setLeft:X(20) to:nil save:YES];
    [point1 lt_setSize:CGSizeMake(6, 6) save:YES];
    point1.layer.cornerRadius = 3;
    point1.clipsToBounds = YES;
    point1.backgroundColor = [UIColor blackColor];
    UILabel *l1 = [[UILabel alloc]init];
    l1.tag = 1;
    [intro addSubview:l1];
    [l1 lt_YcenterTo:nil save:YES];
    [l1 lt_setLeft:X(8) to:point1 save:YES];
    l1.textColor = BLACK;
    l1.font = [UIFont systemFontOfSize:13];
    UIView *point2 = [[UIView alloc]init];
    [intro addSubview:point2];
    [point2 lt_YcenterTo:nil save:YES];
    [point2 lt_setLeft:SCREENWIDTH / 2 + X(10) to:nil save:YES];
    [point2 lt_setSize:CGSizeMake(6, 6) save:YES];
    point2.layer.cornerRadius = 3;
    point2.clipsToBounds = YES;
    point2.backgroundColor = [UIColor blackColor];
    UILabel *l2 = [[UILabel alloc]init];
    l2.tag = 2;
    [intro addSubview:l2];
    [l2 lt_YcenterTo:nil save:YES];
    [l2 lt_setLeft:X(8) to:point2 save:YES];
    l2.textColor = BLACK;
    l2.font = [UIFont systemFontOfSize:13];
    l1.text = [NSString stringWithFormat:@"单笔限额 %@",[dic objectForKey:@"perLimit"]];
    l2.text = [NSString stringWithFormat:@"单日限额 %@",[dic objectForKey:@"dayLimit"]];

    UIView *gray = [[UIView alloc]init];
    [detail addSubview:gray];
    gray.backgroundColor = LINE;
    [gray lt_Top:SunEdgesMake(0, 0, 0, Y(1)) to:intro save:YES];
    UILabel *label = [[UILabel alloc]init];
    [detail addSubview:label];
    [label lt_setLeft:X(24) to:nil Right:X(24) to:nil save:YES];
    [label lt_setTop:Y(28) to:intro save:YES];
    label.font = [UIFont systemFontOfSize:13];
    label.numberOfLines = 0;
    label.preferredMaxLayoutWidth = SCREENWIDTH - X(48);
    label.textColor = GRAY;
    label.text = [NSString stringWithFormat:@"温馨提示:\n资金同卡进出，只能使用一张进行投资及提现，更换银行卡请联系客服%@，为了保证您的账户安全，请同时提供相关身份证明。",KEFU];
    
//    UILabel *l1 = [[UILabel alloc]init];
//    [detail addSubview:l1];
//    [l1 lt_setTop:Y(25) to:nil save:YES];
//    [l1 lt_setLeft:X(32) to:nil save:YES];
//    l1.textColor = BLACK;
//    l1.font = FONT(14);
//    l1.text = @"恒丰银行存管信息";
//    _name = [[UILabel alloc]init];
//    [detail addSubview:_name];
//    [_name lt_leftEqual:l1 save:YES];
//    [_name lt_setTop:Y(10) to:l1 save:YES];
//    _name.textColor = BLACK;
//    _name.font = FONT(14);
//    _name.text = @"开户姓名：";
//    _jxAccount = [[UILabel alloc]init];
//    [detail addSubview:_jxAccount];
//    [_jxAccount lt_leftEqual:_name save:YES];
//    [_jxAccount lt_setTop:Y(10) to:_name save:YES];
//    _jxAccount.textColor = BLACK;
//    _jxAccount.font = FONT(14);
//    _jxAccount.text = @"存管账户：";
//    _jxBank = [[UILabel alloc]init];
//    [detail addSubview:_jxBank];
//    [_jxBank lt_leftEqual:_jxAccount save:YES];
//    [_jxBank lt_setTop:Y(10) to:_jxAccount save:YES];
//    _jxBank.textColor = BLACK;
//    _jxBank.font = FONT(14);
//    _jxBank.text = @"账户开户行：";
//    _bank = [[UILabel alloc]init];
//    [detail addSubview:_bank];
//    [_bank lt_leftEqual:_jxBank save:YES];
//    [_bank lt_setTop:Y(10) to:_jxBank save:YES];
//    [_bank lt_setBottom:Y(25) to:nil save:YES];
//    _bank.textColor = BLACK;
//    _bank.font = FONT(14);
//    _bank.text = @"银行卡绑定：";
//    _btn = [[UIButton alloc]init];
//    [detail addSubview:_btn];
//    [_btn lt_YcenterTo:_bank save:YES];
//    [_btn lt_setRight:X(40) to:nil save:YES];
//    _btn.layer.borderWidth = 0.5;
//    [_btn lt_setSize:CGSizeMake(X(56), Y(22)) save:YES];
//    _btn.layer.cornerRadius = 2;
//    _btn.layer.borderColor = RED.CGColor;
//    [_btn setTitleColor:RED forState:UIControlStateNormal];
//    _btn.titleLabel.font = FONT(10);
//    [_btn addTarget:self action:@selector(changeBank) forControlEvents:UIControlEventTouchUpInside];
//    UIView *bottom = [[UIView alloc]init];
//    [self.view addSubview:bottom];
//    UIImageView *safe = [[UIImageView alloc]init];
//    [bottom addSubview:safe];
//    safe.image = [UIImage imageNamed:@"first_safe"];
//    [safe lt_YcenterTo:nil save:YES];
//    UILabel *safeLabel = [[UILabel alloc]init];
//    [bottom addSubview:safeLabel];
//    safeLabel.text = [Sun getUser:SAFETEXT];
//    safeLabel.font = [UIFont systemFontOfSize:10];
//    safeLabel.textColor = [HXColor colorWithHexString:@"5f606d"];
//    [safeLabel lt_YcenterTo:nil save:YES];
//    [safeLabel lt_setLeft:X(5) to:safe save:YES];
//    [bottom lt_layoutView:bottom side:SunLeft to:safe toSide:SunLeft withSize:1 offset:0 save:NO];
//    [bottom lt_layoutView:bottom side:SunRight to:safeLabel toSide:SunRight withSize:1 offset:0 save:YES];
//    [bottom lt_XcenterTo:nil save:NO];
//    [bottom lt_setBottom:Y(50) to:nil save:NO];
}

- (void)updateViews{
    NSDictionary *dic = [Sun getUser:USER];
    _imgBank1.text = @"存管账户卡片";
    _imgCard1.text = [NSString stringWithFormat:@"%@********%@",[[dic objectForKey:@"jxCard"] substringToIndex:4],[[dic objectForKey:@"jxCard"] substringFromIndex:[[dic objectForKey:@"jxCard"]length] - 4]];
    _name.text = [NSString stringWithFormat:@"开户姓名：%@",[dic objectForKey:@"name"]];
    _jxAccount.text = [NSString stringWithFormat:@"存管账户：%@",[Sun formatStringToBank:[dic objectForKey:@"jxCard"]]];
    _jxBank.text = [NSString stringWithFormat:@"账户开户行：%@",[dic objectForKey:@"jxBank"]];
    if ([[dic objectForKey:@"status"] intValue] == 3) {
        _imgCard2.hidden = NO;
        _imgBank2.text = @"绑定的银行卡";
        _imgBank2.textAlignment = NSTextAlignmentLeft;
        _imgCard2.text = [NSString stringWithFormat:@"%@********%@",[[dic objectForKey:@"bankCard"] substringToIndex:4],[[dic objectForKey:@"bankCard"] substringFromIndex:[[dic objectForKey:@"bankCard"]length] - 4]];
        _bank.text = [NSString stringWithFormat:@"银行卡绑定：%@尾号%@",[dic objectForKey:@"bank"],[[dic objectForKey:@"bankCard"] substringFromIndex:[[dic objectForKey:@"bankCard"]length] - 4]];
        [_btn setTitle:@"解绑" forState:UIControlStateNormal];
    }else {
        _imgCard2.hidden = YES;
        _imgBank2.text = @"未绑定银行卡";
        _imgBank2.textAlignment = NSTextAlignmentCenter;
        [_bank setMarkup:@"银行卡绑定：<font color=\"ff5971\">未绑定银行卡</font>"];
        [_btn setTitle:@"立即绑定" forState:UIControlStateNormal];
    }
}

- (void)requestToUser{
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_USERINFO];
    [HttpRequest postURL:url withParam:nil success:^(id requestDic, NSString *msg) {
        [Sun setUser:requestDic forKey:USER];
        [self updateViews];
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]showMsg:errorInfo];
    }];
}

- (void)changeBank{
    NSDictionary *dic = [Sun getUser:USER];
    if ([[dic objectForKey:@"status"] intValue] == 3){
        [[Sun shareSun]alertAlign:NSTextAlignmentCenter title:@"温馨提示" content:@"当可用余额为0，且没有未结清的标的时，可解绑银行卡，您确定要解绑银行卡？" left:@"确定" right:@"取消" cancel:^{
            NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_UNBIND];
            [[Sun shareSun]showActivity:self];
            [HttpRequest postURL:url withParam:nil success:^(id requestDic, NSString *msg) {
                [[Sun shareSun]showMsg:@"银行卡解绑成功"];
                [[Sun shareSun]hideAll];
                [self requestToUser];
            } failure:^(NSString *errorInfo, int code) {
                [[Sun shareSun]hideAll];
                [[Sun shareSun]showMsg:errorInfo];
            }];
        } sure:^{
            [[Sun shareSun]hideAll];
        } To:self];
    }else {
        SunOpenBindVC *vc = [[SunOpenBindVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
